<?php
/*
 * @copyright   Leyun internet Technology(Shanghai)Co.,Ltd
 * @license     http://www.dzzoffice.com/licenses/license.txt
 * @package     DzzOffice
 * @link        http://www.dzzoffice.com
 * @author      zyx(zyx@dzz.cc)
 */
if(!defined('IN_DZZ') || !defined('IN_ADMIN')) {
	exit('Access Denied');
}
include_once DZZ_ROOT.'./data/extdata/exts.php';
require_once libfile('function/user','','user');
$grouptitle=array('0'=>'全部','-1'=>'仅游客可用','1'=>'成员可用','2'=>'部门管理员可用','3'=>'仅系统管理员可用');

$do=trim($_GET['do']);
$appid=intval($_GET['appid']);
$refer=dreferer();

if(submitcheck('appsubmit')){
		$appurl=addslashes(trim($_GET['appurl']));
		$_GET['appdesc'] = getstr($_GET['appdesc']);
		$_GET['feature'] = getstr($_GET['feature']);
		$_GET['taginput']=str_replace(array(',',"'",'，'),'',trim($_GET['taginput']));
		$_GET['fileextinput']=trim($_GET['fileextinput']);
		if(!empty($_GET['fileextinput'])) $_GET['fileext'][]=$_GET['fileextinput'];
		if(!empty($_GET['taginput'])) $_GET['tag'][]=$_GET['taginput'];
		$setarr=array(
				'appname'=>getstr($_GET['appname'],80,0,0,0,-1),
				'appurl'=>trim($_GET['appurl']),
				'noticeurl'=>trim($_GET['noticeurl']),
				'haveflash'=>intval($_GET['haveflash']),
				'vendor'=>trim($_GET['vendor']),
				'hideInMarket'=>intval($_GET['hideInMarket']),
				'appdesc'=>($_GET['appdesc']),
				'isshow'=>intval($_GET['isshow']),
				'havetask'=>intval($_GET['havetask']),
				'feature'=>$_GET['feature'],
				'fileext'=>$_GET['fileext'],
				'group'=>intval($_GET['group']),
				'open'=>intval($_GET['open']),
				'nodup'=>intval($_GET['nodup'])
			);
		
		//判断依序的参数是否有值
		$msg='';
		if(!$setarr['appname']) $msg.='应用名称不能为空<br>';
		if(!$setarr['appurl']) $msg.='应用地址不能为空';
		
		if(($oappid=DB::result_first("select appid from %t where appurl=%s",array('app_market',$setarr['appurl']))) && $oappid!=$appid){
			$msg.='应用地址已经存在';
		}
		
		if($msg) showmessage($msg);
		
		//处理应用图标
		$iconnew='';
		$target='';
		if($appid){
			$target=DB::result_first("select appico from ".DB::table('app_market')." where appid='{$appid}'");
		}
		if($_FILES['iconnew']) {
			if($_FILES['iconnew']['tmp_name']) {
				
				$ext=strtolower(substr(strrchr($_FILES['iconnew']['name'], '.'), 1, 10));
				if(in_array($ext,array('png','jpeg','jpg','gif','bmp'))){
					if($appico=uploadtolocal($_FILES['iconnew'],'appico',$target)){
						$setarr['appico']=$appico;
					}
				}
			}
		} else {
			if(!$_GET['iconnew']) $_GET['iconnew']='dzz/images/default/icodefault.png';
			if($_GET['iconnew'] && $_GET['iconnew']!=$_G['setting']['attachurl'].$target){
				$ext=strtolower(substr(strrchr($_GET['iconnew'], '.'), 1, 10));
				if(in_array($ext,array('png','jpeg','jpg','gif','bmp'))){
					if($appico=imagetolocal($_GET['iconnew'],'appico',$target)){
						$setarr['appico']=$appico;
					}
				}
			}
		}
		$picids=$_GET['picids'];
		//删除已有图片
		$delete_picids=$_GET['delete_pics'];
		if($delete_picids) app_pic_delete($delete_picids);
		
		if($appid){
			C::t('app_market')->update($appid,$setarr);
		}else{
			$setarr['dateline']=$_G['timestamp'];
			if(!$setarr['appico']) $setarr['appico']='dzz/images/default/icodefault.png';
			$appid=C::t('app_market')->insert($setarr,1);
		}
		//处理标签
		C::t('app_tag')->addtags($_GET['tag'],$appid);
		//更新上传图片的id
		if($picids) C::t('app_pic')->update($picids,array('appid'=>$appid));
		C::t('app_open')->insert_by_exts($appid,$_GET['fileext']);
		
		//处理组织机构
		if($setarr['group']!=1) $orgids=array(); //只有用户可用时才设置部门
		else $orgids=$_GET['orgids']?explode(',',$_GET['orgids']):array();
		C::t('app_organization')->replace_orgids_by_appid($appid,$orgids);
		
		showmessage('do_success',$_GET['refer']);
		
}elseif($do=='upload'){
	$picid = 0;
			$uploadfiles = dzz_app_pic_save($_FILES['attach']);
			if($uploadfiles && is_array($uploadfiles)) {
				$picid = $uploadfiles['picid'];
				$uploadStat = 1;
			} else {
				$uploadStat = $uploadfiles;
			}
				echo "<script>";
				echo "parent.uploadStat = '$uploadStat';";
				echo "parent.picid = $picid;";
				echo "parent.upload();";
				echo "</script>";
			exit();
}else{
	include_once libfile('function/organization');
	$sexts=array();
	foreach($exts as $ext){
		$sexts[]=array('name'=>$ext);
	}
	$fileext_source=htmlspecialchars(json_encode($sexts));
	$orglist=C::t('organization')->fetch_all_by_forgid(0);
	$tags=DB::fetch_all("SELECT tagname FROM %t WHERE hot>0 ORDER BY HOT DESC limit 50",array('app_tag'));
	$tag_source=array();
	foreach($tags as $value){
		$tag_source[]=array('name'=>$value['tagname']);
	}
	$tag_source=htmlspecialchars(json_encode($tag_source));
	$app=array();
	if($app=dstripslashes(C::t('app_market')->fetch($appid))){
		if($app['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $app['appico'])){
			$app['appico']=$_G['setting']['attachurl'].$app['appico'];
		}
		$apptags=array();
		foreach(C::t('app_relative')->fetch_all_by_appid($app['appid']) as $value){
			$apptags[]=$value['tagname'];
		}
		if($apptags) $app['tags']=implode(',',$apptags);
		else $app['tags']='';
		//$app['fileext']=$app['fileext']?explode(',',$app['fileext']):array();
		
		$open=$sel=array();
		$orgids=C::t('app_organization')->fetch_orgids_by_appid($app['appid']);
		if($orgids){
			$sel_org=C::t('organization')->fetch_all($orgids);
			foreach($sel_org as $key=>$value){
				$orgpath=getPathByOrgid($value['orgid']);
				$sel_org[$key]['orgname']=implode('-',array_reverse($orgpath));
				$sel[]=$value['orgid'];
				$arr=array_reverse(array_keys($orgpath));
				array_pop($arr);
				$count=count($arr);
				if($open[$arr[$count-1]]){
					if(count($open[$arr[$count-1]])>$count) $open[$arr[count($arr)-1]]=$arr;
				}else{
					$open[$arr[$count-1]]=$arr;
				}
			}
		}
		$sel=implode(',',$sel);
		$openarr=json_encode(array('orgids'=>$open));
		$piclist=array();
		$list=C::t('app_pic')->fetch_all_by_appid($appid,false,true);
		foreach($list as $value){
			$value['pic'] = getAttachUrl($value);
			$value['dateline']=dgmdate($value['dateline'],'d');
			$piclist[] = $value;
		}
	}else{
		$app=array();
				$app['hideInMarket']=0;
				$app['isshow']=1;
				$app['havetask']=1;
				$app['haveflash']=0;
				$app['group']=1;
	}
	include template('edit');
}
function app_pic_delete($picids){
	if(!is_array($picids)) $picids=array($picids);
	foreach($picids as $picid){
		C::t('app_pic')->delete_by_picid($picid);
	}
}
?>
